var QQMapWX = require('../../resource/utils/qqmap-wx-jssdk.js');
var qqmapsdk;
const app = getApp();
Page({
  data: {
    jx_goods:"",
    page:1,
    limit:10,
    getPosution:'',
    tabbar:{},
    showView: true,
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    cardCur: 0,
    tower: []
  },

  // 用于实现点击“核算”时，来显示与隐藏整个“conts”，这一部分其实是利用了面板的显示与隐藏功能  
  change: function () {
    let that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  close: function () {
    let that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },

  onLoad() {
    // console.log(11111)
    // wx.getStorage({
    //   key: 'access_token',
    //   success(res) {
    //     console.log(res.data)
    //   }
    // })
    // wx.getStorage({
    //   key: 'openid',
    //   success(res) {
    //     console.log(res.data)
    //   }
    // })
    // console.log(11111)
    // var getPosution= app.globalData.getPosution;
    // console.log(app.globalData.getPosution)
    var that = this;
    app.changeTabBar();
    wx.setNavigationBarTitle({
      title: '蜂语网络'
    }),
    this.towerSwiper('tower');
    qqmapsdk = new QQMapWX({ key: '4B7BZ-LD46U-C7FVV-4ZFNG-YVZT3-7MFGJ' });
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        // console.log(res)
        const latitude = res.latitude
        const longitude = res.longitude
        that.qqmap(latitude + ',' + longitude);
      }
    })

    app.util.request({
      url: 'admin/WxGoodsApi/rotation',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res.data.data)
        var datalun = res.data.data
        for (var i = 0; i < datalun.length; i++) {
          datalun[i]['rotation_image'] = app.globalData.url + datalun[i]['rotation_image'];
        }
        that.setData({
          tower: datalun
        })

      }
    });

    app.util.request({
      url: 'admin/WxGoodsApi/jx_goods',
      data: {
        page: this.data.page,
        limit: this.data.limit,
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res)
        var data = res.data.data
        for (var i = 0; i < data.length; i++) {
          data[i]['goods_list_image'] = app.globalData.url + data[i]['goods_list_image'];
        }
        that.setData({
          jx_goods: data
        })
      }
    });

  },
  qqmap: function (location){
    var _this = this;
    qqmapsdk.reverseGeocoder({

      location: location || '', 
      success: function (res){
        // console.log(res.result.address);
        _this.setData({
          getPosution: app.globalData.getPosution= res.result.address
        })
      },
      fail: function (error) {
        console.error(error);
      }
    })
  },
  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      towerList: list
    })
  },

  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  tabbar(e){
    app.tabbar(e);
  },
  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },

  // towerSwiper计算滚动
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.towerList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        towerList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        towerList: list
      })
    }
  },
});